package core.view
{
	import core.Main;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	
	import org.ais.event.TEvent;
	import org.aisy.utimer.UTimer;

	public class ProgressView1 extends VUSprite
	{
		/**
		 * 背景
		 */
		protected var bgMc:MovieClip;
		/**
		 * 索引
		 */
		protected var index:uint;
		/**
		 * 加载进度
		 */
		protected var _w0:Number;
		/**
		 * 播放进度
		 */
		protected var _w1:Number;
		/**
		 * 定时器
		 */
		protected var _utimer:UTimer;
		
		public function ProgressView1(index:uint)
		{
			this.index = index;
			init();
		}
		
		/**
		 * 
		 * 初始化
		 * 
		 */
		protected function init():void
		{
			bgMc = new SWF_PROGRESS_BG1();
			bgMc.mc_0.width = bgMc.mc_1.width = 0;
			
			this.addChild(bgMc);
			
			if (Main.IS.isLocal === true) {
				_utimer = new UTimer();
				_utimer.setDelay(1000);
				_utimer.setTimer(__vProgressHandler);
				_utimer.start();
			}
			else {
				VideoView.getInstance().getNetStream().addStreamEvent(ProgressEvent.PROGRESS, __vProgressHandler);
				VideoView.getInstance().getNetStream().addStreamEvent(Event.COMPLETE, __vCompleteHandler);
			}
			
			Main.IS.stage.addEventListener(Event.ENTER_FRAME, __stageEnterFrameHandler);
			
			__addEvent();
		}
		
		/**
		 * 
		 * 注册事件
		 * 
		 */
		protected function __addEvent():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN, __mouseHandler);
			
			TEvent.newTrigger(VideoView.getInstance().getNetStream().NAME, __triggerHandler);
		}
		
		/**
		 * 
		 * 移除事件
		 * 
		 */
		protected function __removeEvent():void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN, __mouseHandler);
			
			Main.IS.stage.removeEventListener(Event.ENTER_FRAME, __stageEnterFrameHandler);
			
			TEvent.removeTrigger(VideoView.getInstance().getNetStream().NAME, __triggerHandler);
		}
		
		/**
		 * 
		 * 加载进度侦听
		 * @param netStream
		 * 
		 */
		protected function __vProgressHandler(netStream:*):void
		{
			if (null === bgMc) {
				VideoView.getInstance().getNetStream().addStreamEvent(ProgressEvent.PROGRESS, null);
				VideoView.getInstance().getNetStream().addStreamEvent(Event.COMPLETE, null);
				return;
			}
			var _w:Number = (parent.width - x) * VideoView.getInstance().getNetStream().getLoadProgress();
			if (_w === _w0) return;
			_w0 = _w;
			bgMc.mc_0.width = _w;
			if (VideoView.getInstance().getNetStream().getLoadProgress() === 1) {
				if (null !== _utimer) {
					_utimer.clear();
					_utimer = null;
				}
			}
			netStream = null;
		}
		
		/**
		 * 
		 * 加载进度成功侦听
		 * 
		 * @param netStream
		 * 
		 */
		protected function __vCompleteHandler(netStream:*):void
		{
			bgMc.mc_0.width = parent.width - x;
			netStream = null;
		}
		
		/**
		 * 
		 * 鼠标事件侦听
		 * 
		 * @param e
		 * 
		 */
		protected function __mouseHandler(e:MouseEvent):void
		{
			if (Main.IS.isLocal === true) {
				VideoView.getInstance().getNetStream().seekByPoint(this.mouseX / this.width * VideoView.getInstance().getNetStream().getMetaData().duration, 0);
			}
			else {
				VideoView.getInstance().getNetStream().seekIndex = index;
				VideoView.getInstance().getNetStream().seekByPoint(this.mouseX / this.width);
				VideoView.getInstance().getNetStream().addStreamEvent(ProgressEvent.PROGRESS, __vProgressHandler);
				VideoView.getInstance().getNetStream().addStreamEvent(Event.COMPLETE, __vCompleteHandler);
			}
			
			TEvent.trigger(VideoView.getInstance().getNetStream().NAME, "CLEAR_PLAY", index);
			
			e = null;
		}
		
		/**
		 * 
		 * ENTER_FRAME 播放进度
		 * 
		 * @param e
		 * 
		 */
		protected function __stageEnterFrameHandler(e:Event):void
		{
			if (!VideoView.getInstance().getNetStream().getMetaData()) return;
			var _w:Number = (parent.width) * VideoView.getInstance().getNetStream().getPlayProgress() - x;
			if (_w === _w1) return;
			_w1 = _w;
			bgMc.mc_1.width = _w;
		}
		
		/**
		 * 
		 * UNetStreamy 全局侦听
		 * 
		 * @param type
		 * @param data
		 * 
		 */
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "DELETE_INDEX":
					if (index === data) clear();
//					else {
//						bgMc.mc_0.width = (parent.width - x) * VideoView.getInstance().getNetStream().getLoadProgress();
//					}
					break;
				case "NEW_STREAM":
					if (index !== data.index) {
						Main.IS.stage.removeEventListener(Event.ENTER_FRAME, __stageEnterFrameHandler);
						bgMc.mc_1.width = 0;
					}
					break;
				case "CLEAR_PLAY":
					if (index !== data) {
						Main.IS.stage.removeEventListener(Event.ENTER_FRAME, __stageEnterFrameHandler);
						bgMc.mc_1.width = 0;
					}
					else {
						Main.IS.stage.addEventListener(Event.ENTER_FRAME, __stageEnterFrameHandler);
						VideoView.getInstance().getNetStream().addStreamEvent(ProgressEvent.PROGRESS, __vProgressHandler);
						VideoView.getInstance().getNetStream().addStreamEvent(Event.COMPLETE, __vCompleteHandler);
					}
					break;
				case "CLEAR_PROGRESS_ALL":
					clear();
					break;
			}
			type = null;
			data = null;
		}
		
		/**
		 * 
		 * 更新显示
		 * 
		 */
		public function updateView():void
		{
			__vProgressHandler(null);
		}
		
		/**
		 * 
		 * 重置大小
		 * 
		 * @param w
		 * @param h
		 * 
		 */
		override public function setSize(w:Number, h:Number):void
		{
			bgMc.mc_0.width *= w;
			bgMc.mc_1.width *= w;
		}
		
		/**
		 * 
		 * 清空
		 * 
		 */
		override public function clear():void
		{
			__removeEvent();
			if (null !== _utimer) _utimer.clear();
			bgMc = null;
			_utimer = null;
			super.clear();
		}
		
	}
}